package com.zhanghe.mall.service.impl;

import com.zhanghe.mall.pojo.CommodityClassification;
import com.zhanghe.mall.pojo.CommodityClassificationTree;
import com.zhanghe.mall.pojo.repository.CommodityClassficationTreeRepository;
import com.zhanghe.mall.pojo.repository.CommodityClassifictionRepository;
import com.zhanghe.mall.service.dto.AddClassficationDTO;
import com.zhanghe.mall.service.vo.CommodityClassificationTreeVO;
import com.zhanghe.mall.service.CommodityClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * CommodityClassificationServiceImpl
 *
 * @author Clevo
 * @date 2018/7/4
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class CommodityClassificationServiceImpl implements CommodityClassificationService {

    @Autowired
    private CommodityClassficationTreeRepository commodityClassficationTreeRepository;

    @Autowired
    private CommodityClassifictionRepository commodityClassifictionRepository;

    /**
     * 添加分类
     * @param addClassficationDTO 需要添加的分类
     * @param parentTreeId  需要添加的分类的父分类
     * @return   
     * @author Clevo  
     * @date 2018/7/4 22:16
    */
    @Override
    public CommodityClassification addClassificationTree(AddClassficationDTO addClassficationDTO, Long parentTreeId) {
        CommodityClassification commodityClassfication = new CommodityClassification();
        commodityClassfication.setName(addClassficationDTO.getName());
        commodityClassfication = commodityClassifictionRepository.saveAndFlush(commodityClassfication);
        if(parentTreeId!=null){
            commodityClassficationTreeRepository.insertClassficationTreeRepository(commodityClassfication.getId(),parentTreeId);
        }
        commodityClassficationTreeRepository.insertSelf(commodityClassfication.getId(),commodityClassfication.getId(),(long)0);
        return commodityClassfication;
    }

    /**
     *  查询祖先节点是ancestor_id的距离大于distance的子节点
     * @param ancestor_id
     * @param distance
     * @return
     */
    @Override
    public List<CommodityClassificationTree> findByAncestor_IdAndDistanceGreaterThan(Long ancestor_id, Long distance) {
        return commodityClassficationTreeRepository.findByAncestor_IdAndDistanceGreaterThan(ancestor_id,distance);
    }

    /**
     * 查询祖先节点是ancestor_id的距离等于distance的子节点
     * @param ancestor_id
     * @param distance
     * @return
     */
    @Override
    public List<CommodityClassificationTree> findByAncestor_IdAndDistance(Long ancestor_id, Long distance) {
        return commodityClassficationTreeRepository.findByAncestor_IdAndDistance(ancestor_id,distance);
    }

    @Override
    public List<CommodityClassificationTree> getRootClassfication() {
        return commodityClassficationTreeRepository.findRoot();
    }
    @Override
    public List<CommodityClassificationTree> getSubClassfication(Long classficationId){
        return findByAncestor_IdAndDistance(classficationId,1L);
    }

    @Override
    public List<CommodityClassificationTreeVO> getClassificationTree() {
        List<CommodityClassificationTreeVO> result =  CommodityClassificationTreeVO.transformList(getRootClassfication());
        for(CommodityClassificationTreeVO sub:result){
            setSubClassficationTree(sub);
        }
        return result;
    }
    @Override
    public void setSubClassficationTree(CommodityClassificationTreeVO vo){
        vo.setChildren(CommodityClassificationTreeVO.transformList(getSubClassfication(vo.getId())));
        if(vo.getChildren()!=null){
            for(CommodityClassificationTreeVO sub:vo.getChildren()){
                setSubClassficationTree(sub);
            }
        }
    }

    @Override
    public void deleteClassification(Long classificationId) {
        CommodityClassification commodityClassification = commodityClassifictionRepository.findById(classificationId).orElse(null);
        if(commodityClassification!=null){
            //删除这个节点与父节点的关系
            List<CommodityClassificationTree> parenttreelist = commodityClassficationTreeRepository.findByDescendant_Id(classificationId);
            for(CommodityClassificationTree tree:parenttreelist){
                commodityClassficationTreeRepository.delete(tree);
            }

            //找出这个节点的关系
            List<CommodityClassificationTree> treelist = commodityClassficationTreeRepository.findByAncestor_Id(classificationId);
            for(CommodityClassificationTree tree:treelist){
                if(!tree.getDescendant().getId().equals(classificationId)){
                    commodityClassficationTreeRepository.delete(tree);
                    //不是当前节点 继续往下删除
                    deleteClassification(tree.getDescendant().getId());
                }else{
                    //是当前节点 删除关系
                    commodityClassficationTreeRepository.delete(tree);
                }

            }
            //最后 删除该节点
            commodityClassifictionRepository.delete(commodityClassification);
        }

    }
}
